<?php

namespace app\bis\controller;

use think\Controller;

class Login extends Controller
{
    public function index()
    {
        if (\request()->isPost()) {
            $data = input('post.');
            $user = model('bisAccount')->get(['username' => $data['username']]);
            if (!$user || $user->status != 1) {
                $this->error('该用户不存在或未验证通过');
            }
            //密码验证
            if (MD5($data['password'] . $user['code']) != $user['password']) {
                $this->error('密码不正确');
            }
            //更新最后登录时间
            model('bisAccount')->UpdateById(['last_login_time' => date('Y-m-d H:i:s', time())], $user->id);
            //将用户信息保存到session中
            session('account', $user, 'bis');
            //登录成功
            $this->success('登录成功', url('index/index'));
        } else {
            //获取session值
            $account = session('account', '', 'bis');
            if ($account && $account->id) {
                $this->redirect(url('index/index'));
            }
            return $this->fetch();
        }

    }
}